package org.pupil.socket.client;


import org.pupil.socket.MyObject;

import java.io.*;
import java.net.Socket;

/**
 * @Description: 服务发送方
 * @Author: pupil
 * @Date: 2024/07/06 下午 1:46
 */
public class Client {
    public static String host = "localhost"; // 服务提供方的主机名或IP地址
    public static int port = 8081; // 服务提供方的端口

    public static void main(String[] args) throws IOException {
        runWithObject();
    }

    public static void run() throws IOException {
        // 建立连接
        Socket socket = new Socket(host, port);
        System.out.println("Connect to server of " + host + ":" + port);

        // 获取socket输出流
        OutputStream outputStream = socket.getOutputStream();
        // 获取写入器，将数据格式化为文本数据
        PrintWriter out = new PrintWriter(outputStream, true);
        // 设置传出消息
        String messageToSend = "Hello, server Pupil!";
        // 把消息传入写入器
        out.println(messageToSend);

        // 关闭连接
        socket.close();
        System.out.println("发送完毕");
    }

    // 粘包问题
    public static void runWithStickyPackage() throws IOException {
        // 建立连接
        Socket socket = new Socket(host, port);
        System.out.println("Connect to server of " + host + ":" + port);

        // 获取socket输出流
        OutputStream outputStream = socket.getOutputStream();

        // 设置传出消息
        String[] messageToSend = {"Hello,", "server!", "How", "are", "you?"};
        // 分组传输消息模拟粘包
        for (String message : messageToSend) {
            outputStream.write(message.getBytes());
            outputStream.flush(); // 确保立即发送
        }

        // 关闭连接
        socket.close();
        System.out.println("发送完毕");
    }

    // 解决粘包问题
    public static void resolveRunWithStickyPackage() throws IOException {
        // 建立连接
        Socket socket = new Socket(host, port);
        System.out.println("Connect to server of " + host + ":" + port);

        // 获取socket输出流
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

        // 设置传出消息
        String[] messageToSend = {"Hello,", "server!", "How", "are", "you?"};
        // 分组传输消息模拟粘包
        for (String message : messageToSend) {
            outputStream.writeInt(message.length()); // 发送消息长度
            outputStream.writeBytes(message); // 发送消息
            outputStream.flush(); // 确保立即发送
        }

        // 关闭连接
        socket.close();
        System.out.println("发送完毕");
    }

    // 半包问题
    public static void runWithHalfPackage() throws IOException {
        // 建立连接
        Socket socket = new Socket(host, port);
        System.out.println("Connect to server of " + host + ":" + port);

        // 获取socket输出流
        OutputStream outputStream = socket.getOutputStream();
        PrintWriter out = new PrintWriter(outputStream,true);

        // 发送一个长消息，模拟半包问题
        String longMessage = "This is a very long message that will be split into two parts to simulate a half package issue.";
       out.println(longMessage);

        // 关闭连接
        socket.close();
        System.out.println("发送完毕");
    }

    // 发送对象
    public static void runWithObject() throws IOException {
        // 建立连接
        Socket socket = new Socket(host, port);
        System.out.println("Connect to server of " + host + ":" + port);

        // 创建要输出的对象
        MyObject myObject = new MyObject("Hello", "Word");

        // 使用对象输出流发送对象
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        out.writeObject(myObject);

        // 关闭连接
        out.close();
        socket.close();
        System.out.println("发送完毕");
    }

}
